package com.ray.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ray.bean.CommonOrderTemplateBean;
import com.ray.mapper.CommonOrderTemplateMapper;

@Service
public class CommonOrderTemplateService {
	@Autowired
	CommonOrderTemplateMapper commonOrderTemplateMapper;
	
	public List<CommonOrderTemplateBean> getTemplates(Map<String, String> map){
		List<CommonOrderTemplateBean> templates = commonOrderTemplateMapper.getTemplates(map);
		return templates;
	}
	
	/**
	 * 保存模板
	 * @param map
	 */
	public void saveTemplate(Map<String, String> map) {
		List<CommonOrderTemplateBean> templates = this.getTemplates(map);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date = dateFormat.format(new Date());
		if (templates.size() == 0) {
			map.put("createTime", date);
			int row = commonOrderTemplateMapper.addTemplate(map);
			Assert.isTrue(row>0, "添加模板失败");
		} else {
			map.put("updateTime", date);
			commonOrderTemplateMapper.updateTemplate(map);
		}
	}
}
